﻿using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class PermissionsController : Controller
    {
        private IPermissionsService _permissionsService;

        public PermissionsController(IPermissionsService permissionsService)
        {
            _permissionsService = permissionsService;
        }
       
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList(int start, int length, string name)
        {
            //1、接口层写分页空方法
            //2、service实现接口方法
            //3、控制器调用
            int count = 0;
            var list = _permissionsService.GetPageList(start, length, name, ref count);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsTotal = count;
            pageModel.recordsFiltered = count;
            return Json(pageModel);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddPer(PermissionDTO dto)
        {
            var result = _permissionsService.Add(dto);
            return Json(result);
        }

        public ActionResult Edit(long id)
        {
           PermissionDTO dto= _permissionsService.Get(id);

            return View(dto);
        }

        [HttpPost]
        public ActionResult UpdatePer(PermissionDTO dto)
        {
            var result = _permissionsService.Update(dto);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(long id)
        {
            var result = _permissionsService.Delete(id);
            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _permissionsService.DeleteBatch(ids);
            return Json(result);
        }
    }
}